package org.sean.framework.feign;

import org.sean.framework.bean.BaseResult;
import org.sean.framework.code.StatusInfo;
import org.sean.framework.exception.StatusException;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "demo", fallback = DemoClient.DefaultFallback.class)
public interface DemoClient {

    @GetMapping("normal")
    public BaseResult<User> normal();

    @GetMapping("excetpion")
    public BaseResult<User> excetpion();

    @GetMapping("callNormal")
    public BaseResult<User> callNormal();

    @GetMapping("callException")
    public BaseResult<User> callException();

    @GetMapping("noBaseResult")
    public User noBasesult();

    @GetMapping("sleep")
    public BaseResult<User> sleep(@RequestParam("s") int s);

    @GetMapping("callSleep")
    public BaseResult<User> callSleep(@RequestParam("s") int s);

    @Component
    class DefaultFallback implements DemoClient {

        @Override
        public BaseResult<User> normal() {
            return new BaseResult<>(1, "normal");
        }

        @Override
        public BaseResult<User> excetpion() {
            return new BaseResult<>(1, "excetpion");
        }

        @Override
        public BaseResult<User> callNormal() {
            return new BaseResult<>(1, "callNormal");
        }

        @Override
        public BaseResult<User> callException() {
            return new BaseResult<>(1, "callException");
        }

        @Override
        public User noBasesult() {
            return User.builder().phone("tesst").name("noBaseResult").build();
        }

        @Override
        public BaseResult<User> sleep(int s) {
            throw new StatusException(StatusInfo.accessMsError());
//            return new BaseResult<>(1, "sleep");
        }

        @Override
        public BaseResult<User> callSleep(int s) {
            throw new StatusException(StatusInfo.accessMsError());
//            return new BaseResult<>(1, "callSleep");
        }
    }
}
